package com.dkts.oil.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.util.*;

public class GeoJsonGenerator {

    public static void main(String[] args) throws Exception {
        File csvFile = new File("C:\\Users\\heyou\\Documents\\WeChat Files\\wxid_86da35bc9zvi21\\FileStorage\\File\\2025-05\\c9a6717b7d2f5293cf7cd2ed05a0494c_2e2437ef9cd58c0d39af05569deeb4be_8.csv");
        Reader reader = new FileReader(csvFile);

        CSVParser parser = CSVFormat.DEFAULT
                .withFirstRecordAsHeader()
                .parse(reader);

        Map<String, List<CSVRecord>> mmsiMap = new HashMap<>();
        for (CSVRecord record : parser) {
            String mmsi = record.get("mmsi");
            if (!mmsiMap.containsKey(mmsi)) {
                mmsiMap.put(mmsi, new ArrayList<>());
            }
            mmsiMap.get(mmsi).add(record);
        }

        ObjectMapper mapper = new ObjectMapper();
        ObjectNode featureCollection = mapper.createObjectNode();
        featureCollection.put("type", "FeatureCollection");
        ArrayNode features = mapper.createArrayNode();

        for (Map.Entry<String, List<CSVRecord>> entry : mmsiMap.entrySet()) {
            List<CSVRecord> records = entry.getValue();
            if (records.size() < 2) continue; // 轨迹少于两个点跳过

            records.sort(Comparator.comparing(r -> r.get("time"))); // 按时间排序

            ObjectNode feature = mapper.createObjectNode();
            feature.put("type", "Feature");

            // 构建 LineString
            ObjectNode geometry = mapper.createObjectNode();
            geometry.put("type", "LineString");
            ArrayNode coordinates = mapper.createArrayNode();
            for (CSVRecord record : records) {
                ArrayNode coord = mapper.createArrayNode();
                coord.add(Double.parseDouble(record.get("lon")));
                coord.add(Double.parseDouble(record.get("lat")));
                coordinates.add(coord);
            }
            geometry.set("coordinates", coordinates);
            feature.set("geometry", geometry);

            // 属性信息
            ObjectNode properties = mapper.createObjectNode();
            CSVRecord first = records.get(0);
            properties.put("mmsi", first.get("mmsi"));
            properties.put("imo", first.get("imo"));
            properties.put("nationality", first.get("nationality"));
            properties.put("name", first.get("name"));
            properties.put("callsign", first.get("callsign"));
            properties.put("length", first.get("length"));
            properties.put("width", first.get("width"));
            properties.put("left", first.get("left"));
            properties.put("tail", first.get("tail"));
            properties.put("draught", first.get("draught"));
            properties.put("dest", first.get("dest"));
            properties.put("eta", first.get("eta"));
            properties.put("shiptype", first.get("shiptype"));
            properties.put("eta_std", first.get("eta_std"));

            // 起止时间
            properties.put("start_time", records.get(0).get("time"));
            properties.put("end_time", records.get(records.size() - 1).get("time"));

            // 轨迹点信息
            ArrayNode trackPoints = mapper.createArrayNode();
            for (CSVRecord record : records) {
                ObjectNode point = mapper.createObjectNode();
                point.put("lon", Double.parseDouble(record.get("lon")));
                point.put("lat", Double.parseDouble(record.get("lat")));
                point.put("time", record.get("time"));
                point.put("speed", record.get("speed"));
                point.put("cog", record.get("cog"));
                point.put("hdg", record.get("hdg"));
                point.put("rot", record.get("rot"));
                point.put("navistat", record.get("navistat"));
                trackPoints.add(point);
            }
            properties.set("track_points", trackPoints);

            feature.set("properties", properties);
            features.add(feature);
        }

        featureCollection.set("features", features);

        FileWriter writer = new FileWriter("output.geojson");
        mapper.writerWithDefaultPrettyPrinter().writeValue(writer, featureCollection);
        System.out.println("GeoJSON 文件已生成：output.geojson");

    }
}
